import request from './api'
// 统一对账单API服务
class UnifiedBillingApi {
  constructor() {
    // 注入Vuex的userType getter
    this.userType = null
  }

  // 设置用户类型
  setUserType(userType) {
    this.userType = userType
  }

  // 获取对账单列表
  getBillingList(pageNum, pageSize, filters = {}) {
    if (this.userType === 'supplier') {
      return request({
        url: '/supplier/billing/my-list',
        method: 'get',
        params: {
          pageNum,
          pageSize,
          status: filters.status
        }
      })
    } else if (this.userType === 'yunying') {
      return request({
        url: '/yunying/billing/list',
        method: 'get',
        params: {
          pageNum,
          pageSize,
          supplierId: filters.supplierId ? Number(filters.supplierId) : null,
          status: filters.status,
          startDate: filters.startDate,
          endDate: filters.endDate
        }
      })
    }
    return Promise.reject(new Error('未知用户类型'))
  }

  // 获取对账单详情
  getBillingDetail(id) {
    if (this.userType === 'supplier') {
      return request({
        url: `/supplier/billing/my-detail/${id}`,
        method: 'get'
      })
    } else if (this.userType === 'yunying') {
      return request({
        url: `/yunying/billing/detail/${id}`,
        method: 'get'
      })
    }
    return Promise.reject(new Error('未知用户类型'))
  }

  // 获取对账单明细
  getBillingItems(statementId, pageNum, pageSize) {
    if (this.userType === 'supplier') {
      return request({
        url: `/supplier/billing/my-items/${statementId}`,
        method: 'get',
        params: {
          pageNum,
          pageSize
        }
      })
    } else if (this.userType === 'yunying') {
      return request({
        url: `/yunying/billing/items/${statementId}`,
        method: 'get',
        params: {
          pageNum,
          pageSize
        }
      })
    }
    return Promise.reject(new Error('未知用户类型'))
  }

  // 确认对账单 (供应商)
  confirmBilling(id) {
    if (this.userType === 'supplier') {
      return request({
        url: `/supplier/billing/confirm/${id}`,
        method: 'post'
      })
    }
    return Promise.reject(new Error('只有供应商可以执行此操作'))
  }

  // 提交对账单争议 (供应商)
  disputeBilling(id, reason) {
    if (this.userType === 'supplier') {
      return request({
        url: `/supplier/billing/dispute/${id}`,
        method: 'post',
        params: {
          reason
        }
      })
    }
    return Promise.reject(new Error('只有供应商可以执行此操作'))
  }

  // 生成对账单 (运营)
  generateBilling(data) {
    if (this.userType === 'yunying') {
      return request({
        url: '/yunying/billing/generate',
        method: 'post',
        data
      })
    }
    return Promise.reject(new Error('只有运营人员可以执行此操作'))
  }

  // 批量生成对账单 (运营)
  batchGenerateBillings(data) {
    if (this.userType === 'yunying') {
      return request({
        url: '/yunying/billing/batch-generate',
        method: 'post',
        data
      })
    }
    return Promise.reject(new Error('只有运营人员可以执行此操作'))
  }

  // 更新对账单状态 (运营)
  updateBillingStatus(data) {
    if (this.userType === 'yunying') {
      return request({
        url: '/yunying/billing/update-status',
        method: 'post',
        data
      })
    }
    return Promise.reject(new Error('只有运营人员可以执行此操作'))
  }
}

export default new UnifiedBillingApi()